﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HexEditor_
{
    public class HexEditor
    {
        public DataTypeManager _DataTypeManager { get; set; }
        List<HexDoc> _HexDocList;
        bool _SingleDoc;

        public static string LastErrorMsg { get; set; }

        public HexDoc GetDoc(int _idx)
        {
            if (_idx < _HexDocList.Count)
                return _HexDocList[_idx];
            return null;
        }
        public void Initialize(bool bSingleDoc)
        {
            if (_DataTypeManager == null)
            {
                _DataTypeManager = new DataTypeManager();
                _DataTypeManager.Initialize();
            }
            if (_HexDocList == null)
            {
                _HexDocList = new List<HexDoc>();
            }
            _SingleDoc = bSingleDoc;
        }

        public HexDoc FindDoc(string strPath)
        {
            return null;
        }

        public HexDoc OpenFile(string strPath)
        {
            HexDoc doc = FindDoc(strPath);
            
            if(doc != null)
                return doc;

            if (_SingleDoc)
                CloseAll();

            doc = new HexDocNormal();
            if (doc.Open(strPath))
            {
                doc.SetFormatRange(this,_DataTypeManager.DefaultDataType, -1, -1);
                _HexDocList.Add(doc);
                return doc;
            }
            return null;
        }

        public void Close(int _idx)
        {
            _HexDocList[_idx].Close();
            _HexDocList.RemoveAt(_idx);
        }

        public void CloseAll()
        {
            int num = _HexDocList.Count;
            for (int i = 0; i < num; ++i)
            {
                _HexDocList[i].Close();
            }
            _HexDocList.Clear();
        }
    }
}
